1
|
|
|
/** |
2
|
|
|
* Test movie |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
const ServerMock = require('mock-http-server') |
6
|
|
|
const expect = require('chai').expect |
7
|
|
|
const movie = require('../bin/movies') |
8
|
|
|
|
9
|
|
|
const serve = new ServerMock({ |
10
|
|
|
host: 'localhost', |
11
|
|
|
port: 3000 |
12
|
|
|
}) |
13
|
|
|
|
14
|
|
|
const req = () => { |
15
|
|
|
serve.on({ |
16
|
|
|
method: 'GET', |
17
|
|
|
path: '/response', |
18
|
|
|
reply: { |
19
|
|
|
status: 200, |
20
|
|
|
headers: {'content-type': 'application/json'}, |
21
|
|
|
body: JSON.stringify({ |
22
|
|
|
'data': { |
23
|
|
|
'movies': [ |
24
|
|
|
{ |
25
|
|
|
'id': 1, |
26
|
|
|
'title': 'Star Wars', |
27
|
|
|
'cover': 'Movie cover', |
28
|
|
|
'rating': '9.7', |
29
|
|
|
'genres': ['Action'], |
30
|
|
|
'torrents': [ |
31
|
|
|
{ |
32
|
|
|
'quality': '3D' |
33
|
|
|
} |
34
|
|
|
] |
35
|
|
|
} |
36
|
|
|
] |
37
|
|
|
} |
38
|
|
|
}) |
39
|
|
|
} |
40
|
|
|
}) |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
describe('Movie', function () { |
44
|
|
|
beforeEach(function (done) { |
45
|
|
|
serve.start(done) |
46
|
|
|
}) |
47
|
|
|
|
48
|
|
|
afterEach(function (done) { |
49
|
|
|
serve.stop(done) |
50
|
|
|
}) |
51
|
|
|
|
52
|
|
|
before(function () { |
53
|
|
|
movie.setEndpoint({list: 'http://localhost:3000/response'}) |
54
|
|
|
}) |
55
|
|
|
|
56
|
|
|
it ('expect to be an error if endpoint is not an object', function () { |
57
|
|
|
expect(() => { |
58
|
|
|
movie.setEndpoint('string') |
59
|
|
|
}).to.throw('endpoint must be an object') |
60
|
|
|
}) |
61
|
|
|
|
62
|
|
|
it ('expect to be an error if param is not an object', function () { |
63
|
|
|
expect(() => { |
64
|
|
|
movie.setParams('string') |
65
|
|
|
}).to.throw('params must be an object') |
66
|
|
|
}) |
67
|
|
|
|
68
|
|
|
it ('expect return an array', function () { |
69
|
|
|
req() |
70
|
|
|
|
71
|
|
|
return movie.get() |
72
|
|
|
.then(response => { |
73
|
|
|
expect(response.data.movies).to.be.an('array') |
74
|
|
|
}) |
75
|
|
|
}) |
76
|
|
|
|
77
|
|
|
it ('expect return an array and contains genres with action property', function () { |
78
|
|
|
req() |
79
|
|
|
|
80
|
|
|
return movie.findByGenre('action') |
81
|
|
|
.then(function (response) { |
82
|
|
|
expect(response.data.movies[0]) |
83
|
|
|
.to.be.an('object') |
84
|
|
|
.to.have.deep.property('genres[0]', 'Action') |
85
|
|
|
}) |
86
|
|
|
}) |
87
|
|
|
|
88
|
|
|
it ('expect return an array and contains quality with 3D property', function () { |
89
|
|
|
req() |
90
|
|
|
|
91
|
|
|
return movie.findByQuality('3D') |
92
|
|
|
.then(function (response) { |
93
|
|
|
expect(response.data.movies[0]) |
94
|
|
|
.to.be.an('object') |
95
|
|
|
.to.have.deep.property('torrents[0].quality', '3D') |
96
|
|
|
}) |
97
|
|
|
}) |
98
|
|
|
|
99
|
|
|
it ('expect return an array and contain rating higher than argument', function () { |
100
|
|
|
req() |
101
|
|
|
|
102
|
|
|
return movie.findByRating('8') |
103
|
|
|
.then(function (response) { |
104
|
|
|
expect(response.data.movies[0].rating).to.be.above(8) |
105
|
|
|
}) |
106
|
|
|
}) |
107
|
|
|
|
108
|
|
|
it ('expect return to be an array and have title match with Star Wars', function () { |
109
|
|
|
req() |
110
|
|
|
|
111
|
|
|
return movie.search('Star Wars') |
112
|
|
|
.then(response => { |
113
|
|
|
expect(response.data.movies[0].title).to.match(/Star Wars/g) |
114
|
|
|
}) |
115
|
|
|
}) |
116
|
|
|
}) |
117
|
|
|
|